package homework7;


import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


// 可充电接口
interface Chargeable {
    void charge();
    int getBatteryLevel();
    String getChargingMethod();
    List<ChargeHistory> getChargeHistory();
}


// 充电历史记录类
class ChargeHistory {
    private LocalDateTime time;
    private int chargedAmount;


    public ChargeHistory(LocalDateTime time, int chargedAmount) {
        this.time = time;
        this.chargedAmount = chargedAmount;
    }


    public LocalDateTime getTime() {
        return time;
    }


    public int getChargedAmount() {
        return chargedAmount;
    }


    @Override
    public String toString() {
        return "ChargeHistory{" +
                "time=" + time +
                ", chargedAmount=" + chargedAmount +
                '}';
    }
}


// 手机类，实现 Chargeable 接口
class Phone implements Chargeable {
    private int batteryLevel;
    private String chargingMethod;
    private List<ChargeHistory> chargeHistory;


    public Phone(int batteryLevel, String chargingMethod) {
        this.batteryLevel = batteryLevel;
        this.chargingMethod = chargingMethod;
        this.chargeHistory = new ArrayList<>();
    }


    @Override
    public void charge() {
        int chargedAmount = calculateChargingEfficiency();
        batteryLevel += chargedAmount;
        if (batteryLevel > 100) {
            batteryLevel = 100;
        }
        chargeHistory.add(new ChargeHistory(LocalDateTime.now(), chargedAmount));
        System.out.println("Phone is charging using " + chargingMethod + ". Battery level: " + batteryLevel);
    }


    @Override
    public int getBatteryLevel() {
        return batteryLevel;
    }


    @Override
    public String getChargingMethod() {
        return chargingMethod;
    }


    @Override
    public List<ChargeHistory> getChargeHistory() {
        return chargeHistory;
    }


    private int calculateChargingEfficiency() {
        if (chargingMethod.equals("fast")) {
            return 20;
        } else if (chargingMethod.equals("wireless")) {
            return 10;
        } else {
            return 5;
        }
    }


    public String getBatteryStatus() {
        if (batteryLevel < 30) {
            return "low";
        } else if (batteryLevel < 70) {
            return "medium";
        } else {
            return "full";
        }
    }
}


// 平板电脑类，实现 Chargeable 接口
class Tablet implements Chargeable {
    private int batteryLevel;
    private String chargingMethod;
    private List<ChargeHistory> chargeHistory;


    public Tablet(int batteryLevel, String chargingMethod) {
        this.batteryLevel = batteryLevel;
        this.chargingMethod = chargingMethod;
        this.chargeHistory = new ArrayList<>();
    }


    @Override
    public void charge() {
        int chargedAmount = calculateChargingEfficiency();
        batteryLevel += chargedAmount;
        if (batteryLevel > 100) {
            batteryLevel = 100;
        }
        chargeHistory.add(new ChargeHistory(LocalDateTime.now(), chargedAmount));
        System.out.println("Tablet is charging using " + chargingMethod + ". Battery level: " + batteryLevel);
    }


    @Override
    public int getBatteryLevel() {
        return batteryLevel;
    }


    @Override
    public String getChargingMethod() {
        return chargingMethod;
    }


    @Override
    public List<ChargeHistory> getChargeHistory() {
        return chargeHistory;
    }


    private int calculateChargingEfficiency() {
        if (chargingMethod.equals("fast")) {
            return 30;
        } else if (chargingMethod.equals("wireless")) {
            return 15;
        } else {
            return 8;
        }
    }


    public String getBatteryStatus() {
        if (batteryLevel < 30) {
            return "low";
        } else if (batteryLevel < 70) {
            return "medium";
        } else {
            return "full";
        }
    }
}


// 主类，用于测试
public class chongdian {
    public static void main(String[] args) {
        // 创建一个手机对象并测试
        Phone phone = new Phone(20, "fast");
        System.out.println("Initial battery level: " + phone.getBatteryLevel());
        System.out.println("Initial battery status: " + phone.getBatteryStatus());
        phone.charge();
        phone.charge();
        System.out.println("Final battery level: " + phone.getBatteryLevel());
        System.out.println("Final battery status: " + phone.getBatteryStatus());
        System.out.println("Charge history: " + phone.getChargeHistory());


        // 创建一个平板电脑对象并测试
        Tablet tablet = new Tablet(40, "wireless");
        System.out.println("Initial battery level: " + tablet.getBatteryLevel());
        System.out.println("Initial battery status: " + tablet.getBatteryStatus());
        tablet.charge();
        tablet.charge();
        tablet.charge();
        System.out.println("Final battery level: " + tablet.getBatteryLevel());
        System.out.println("Final battery status: " + tablet.getBatteryStatus());
        System.out.println("Charge history: " + tablet.getChargeHistory());
    }
}
